package controllers;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;

import javax.persistence.TypedQuery;

import models.Ingredient;
import models.Product;
import models.Salad;
import models.User;
import models.VeggieSalad;
import play.db.jpa.Blob;
import play.db.jpa.GenericModel;
import play.mvc.Controller;

public class SaladController extends Controller {

	public static String CUSTOM_SALAD_ID = "customSaladId";
	
	/**
	 * Retrieves the ingredients to display in the salad.
	 */
    public static void ingredients() {
    	TypedQuery<Ingredient> query = GenericModel.em().createQuery("SELECT c FROM Ingredient c WHERE category = 'LETTUCE'", Ingredient.class);
		List<Ingredient> lettuces = query.getResultList();
		query = GenericModel.em().createQuery("SELECT c FROM Ingredient c WHERE category = 'TOPPING' OR category = 'FRUIT'", Ingredient.class);
    	List<Ingredient> toppings = query.getResultList();
    	query = GenericModel.em().createQuery("SELECT c FROM Ingredient c WHERE category = 'PROTEIN'", Ingredient.class);
    	List<Ingredient> proteins = query.getResultList();
    	query = GenericModel.em().createQuery("SELECT c FROM Ingredient c WHERE category = 'DRESSING'", Ingredient.class);
    	List<Ingredient> dressings = query.getResultList();
    	
    	Salad salad = null;
    	if (session.get(CUSTOM_SALAD_ID) != null) {
			Long saladId = Long.parseLong(session.get(CUSTOM_SALAD_ID));
			salad = Salad.findById(saladId);
    	}
    	
        render(lettuces, dressings, proteins, toppings, salad);
    }
    
    /**
	 * Gets the image in a blob. Not functional yet.
	 * @param id
	 */
	public static void getImage(Long id) {
		Ingredient product = Ingredient.findById(id);        
		renderBinary(product.icon.get());
	}
	
	/**
	 * Creates the salad
	 * @param selectedLettuces
	 * @param selectedToppings
	 * @param selectedProteins
	 * @param selectedDressings
	 * @throws Exception
	 */
    public static void createSalad(List<String> selectedLettuces, List<String> selectedToppings, List<String> selectedProteins, List<String> selectedDressings) throws Exception{
    	
    	if (session.get(CUSTOM_SALAD_ID) != null) {
    		User user = Application.getCurrentUser();
    		Long saladId = Long.parseLong(session.get(CUSTOM_SALAD_ID));
    		Salad salad = Salad.findById(saladId);
    		if (user != null)
    			salad.creator = user;
        	salad.creationDate = new Date();
        	Blob blob =new Blob();
        	FileInputStream stream = new FileInputStream(new File("."+File.separatorChar+"public"+File.separatorChar+"img"+File.separatorChar+"salad4.JPG"));    	
        	blob.set(stream, "jpg");
        	salad.icon = blob;
        	salad.name += ("-"+salad.ingredients.get(0).name); 
        	salad.save();
        	stream.close();
			DataManager.addLineCart(Application.getShoppingCart(), saladId);
			session.remove(CUSTOM_SALAD_ID);
    	}
    	
    	Application.index2();
    }    
    
    /**
     * Adds the ingredient to the salad
     * @param ingredientId
     */
    public static void addToSalad(long ingredientId, boolean checked) {
    	long id = DataManager.addToTemporarySalad(session.get(CUSTOM_SALAD_ID), ingredientId, checked);
    	session.put(CUSTOM_SALAD_ID, id);
    }
    
    /**
     * Returns the custom salad that is being build.
     */
    public static void customSalad(){
    	Salad salad = null;
    	if (session.get(CUSTOM_SALAD_ID) != null) {
			Long saladId = Long.parseLong(session.get(CUSTOM_SALAD_ID));
			salad = Salad.findById(saladId);
    	}
    	render(salad);
    }
}
